<template>
  <div class="login">
    <a-form :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-item label="用户名" v-bind="validateInfos.name">
        <a-input
          v-model:value="modelRef.name"
          @blur="validate('name', { trigger: 'blur' }).catch(() => {})"
        />
      </a-form-item>
      <a-form-item label="密码" v-bind="validateInfos.password">
      <a-input
          v-model:value="modelRef.password"
          @blur="validate('password', { trigger: 'blur' }).catch(() => {})"
        />
      </a-form-item>
      <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
        <a-button type="primary" @click.prevent="onSubmit">登录</a-button>
        <a-button style="margin-left: 10px" @click="resetFields">重置</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts">
import { defineComponent, reactive } from 'vue';
import { useForm } from '@ant-design-vue/use';
import { useStore } from "vuex";
import { IGlobalState } from '@/store';
import * as Types from '@/store/action-types'
import { useRouter } from 'vue-router'
export default defineComponent({
  setup() {
    let store = useStore<IGlobalState>();
    let router = useRouter();
    const modelRef = reactive({
      name: 'dengjian',
      password: '123456',
    });
    const rulesRef = reactive({
      name: [
        {
          required: true,
          message: 'Please input Activity name',
        },
        {
          min: 3,
          max: 10,
          message: 'Length should be 3 to 10',
          trigger: 'blur',
        },
      ],
      password: [
        {
          required: true
        },
      ],
    });
    const { resetFields, validate, validateInfos } = useForm(modelRef, rulesRef);
    const onSubmit = () => {
      validate()
        .then(() => {
          console.log('登录成功了')
          // 这里拿到用户的信息
          if(modelRef.name === 'dengjian') {
            store.commit(`user/${Types.SET_USER_INFO}`, {
              name: 'dengjain',
              id: 0,
              auth: ['personCenter','about','switch','transfer','select']
            })
            store.commit(`user/${Types.SET_USER_PERMISSIONS}`, {
              auth: [
                {
                  menuCode: 'personCenter',
                  btnRoles: ['add','delete']
                },
                {
                  menuCode: 'about',
                  btnRoles: ['add','delete']
                },
                {
                  menuCode: 'switch',
                  btnRoles: ['add','delete']
                },
                {
                  menuCode: 'transfer',
                  btnRoles: ['add','delete']
                },
                {
                  menuCode: 'select',
                  btnRoles: ['add','delete']
                }
              ]
            })
          } else {
            store.commit(`user/${Types.SET_USER_INFO}`, {
              name: 'dengjain',
              id: 0,
            })
            store.commit(`user/${Types.SET_USER_PERMISSIONS}`, {
              auth: [
                {
                  menuCode: 'personCenter',
                  btnRoles: ['add','delete']
                },
                {
                  menuCode: 'about',
                  btnRoles: ['add','delete']
                }
              ]
            })
          }
          router.push('home')
        })
        .catch(err => {
          console.log('error', err);
        });
    };
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      validate,
      validateInfos,
      resetFields,
      modelRef,
      onSubmit,
    };
  },
});
</script>
<style scoped>
.login{
  padding-top: 200px;
}
</style>